#!/bin/bash

LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
function ExitCleanup {
    # keep the log file, but ensure a different user can overwrite it next time
    cd /
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" == "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    ExitCleanup 1
fi

# Check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    ExitCleanup 2
fi

# Remove the zip file to save space
rm -f /usr/local/hsc_install.images/*.zip

# Save old log files
if [ -f $LogFile ]; then
    mv -f ${LogFile}.3 ${LogFile}.4 2>/dev/null
    mv -f ${LogFile}.2 ${LogFile}.3 2>/dev/null
    mv -f ${LogFile}.1 ${LogFile}.2 2>/dev/null
    mv -f ${LogFile}   ${LogFile}.1 2>/dev/null
fi

echo "=========================================================" >> $LogFile
echo "*********   Performing Update operation `date`  *********" >> $LogFile
echo "=========================================================" >> $LogFile

if [ -f $image/SnmCA.pem ]; then
    echo "Updating CA Certificates" 
    echo "Updating CA Certificates" >> $LogFile
    cp $image/SnmCA.pem /opt/hsc/data/certs
fi

echo "==========================================================" >> $LogFile
echo "********* Install/Update complete at `date` *********"      >> $LogFile
echo "==========================================================" >> $LogFile
ExitCleanup 0
